package com.zgjkhis.modules.system.rest;

import cn.hutool.poi.excel.ExcelFileUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.zgjkhis.domain.PageResult;
import com.zgjkhis.exception.ServiceException;
import com.zgjkhis.logging.aop.log.Log;
import com.zgjkhis.modules.security.security.LoginUserUtil;
import com.zgjkhis.modules.system.domain.DrugInventory;
import com.zgjkhis.modules.system.domain.DrugInventoryDetail;
import com.zgjkhis.modules.system.domain.vo.DrugInventoryVo;
import com.zgjkhis.modules.system.req.InventoryExamineReq;
import com.zgjkhis.modules.system.service.DrugInventoryDetailService;
import com.zgjkhis.modules.system.service.DrugInventoryService;
import com.zgjkhis.modules.system.service.DrugWareHousingDetailService;
import com.zgjkhis.modules.system.service.dto.DrugInventoryQueryCriteria;
import com.zgjkhis.modules.system.service.dto.DrugTypeQueryCriteria;
import com.zgjkhis.modules.util.CharUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * @date 2018-11-23
 */
@Api(tags = "库房管理：盘点数据")
@RestController
@RequestMapping("/drugInventory")
public class DrugInventoryController {

    private static final String ENTITY_NAME = "drugInventory";
    private final DrugInventoryService drugInventoryService;
    private final DrugWareHousingDetailService drugWareHousingDetailService;
    private final DrugInventoryDetailService drugInventoryDetailService;

    public DrugInventoryController(DrugInventoryService drugInventoryService, DrugWareHousingDetailService drugWareHousingDetailService, DrugInventoryDetailService drugInventoryDetailService) {
        this.drugInventoryService = drugInventoryService;
        this.drugWareHousingDetailService = drugWareHousingDetailService;
        this.drugInventoryDetailService = drugInventoryDetailService;
    }


    @Log("根据药品分类下载盘点数据")
    @ApiOperation("根据药品分类下载盘点数据")
    @PostMapping(value = "/downloadDrugForInventory")
//    @PreAuthorize("@el.check('drug:list')")
    public void downloadDrugByType(HttpServletResponse response, DrugTypeQueryCriteria criteria) throws IOException {
        String pharmacyId = LoginUserUtil.getPharmId();
        criteria.setPharmacyId(pharmacyId);
        drugInventoryService.dowonload(drugWareHousingDetailService.findAll(criteria), response);
    }

    @Log("导入盘点数据")
    @ApiOperation("导入盘点数据")
    @PostMapping(value = "/batchInventory")
//    @PreAuthorize("@el.check('drug:list')")
    public ResponseEntity<Object> batchInventory(@RequestParam("file") MultipartFile file) throws IOException {
        if (file == null) {
            throw new ServiceException("请上传文件");
        }
        if (!ExcelFileUtil.isXls(file.getInputStream()) && !ExcelFileUtil.isXlsx(file.getInputStream())) {
            throw new ServiceException("请上传excel文件");
        }
        drugInventoryService.batchInventory(file);
        return new ResponseEntity<>(true, HttpStatus.OK);
    }

    @Log("分页查询盘点信息")
    @ApiOperation("分页查询盘点信息")
    @PostMapping(value = "/getList")
//    @PreAuthorize("@el.check('drugWareHousing:list')")
    public ResponseEntity<PageResult<DrugInventory>> getList(DrugInventoryQueryCriteria criteria, Pageable pageable) {
        criteria.setPharmacyId(LoginUserUtil.getPharmId());
        return new ResponseEntity<>(drugInventoryService.findAllByPageable(criteria, pageable), HttpStatus.OK);
    }

    @Log("查看盘点")
    @GetMapping(value = "/detail")
    @ApiOperation("查看盘点")
    //    @PreAuthorize("@el.check('drugWareHousing:datail')")
    public ResponseEntity<DrugInventoryVo> getDetail(@ApiParam(value = "盘点单id") String id) {
        if (CharUtil.isEmpty(id)) {
            throw new ServiceException("id不能为空");
        }
        return new ResponseEntity<>(this.drugInventoryService.getDetail(id), HttpStatus.OK);
    }

    @Log("导出盘点单")
    @ApiOperation("导出盘点单")
    @PostMapping(value = "/downloadInventoryDetail")
//    @PreAuthorize("@el.check('drug:list')")
    public void downloadInventoryDetail(HttpServletResponse response, @ApiParam(value = "盘点单id") String id) throws IOException {
        if (CharUtil.isEmpty(id)) {
            throw new ServiceException("id不能为空");
        }
        DrugInventory drugInventory = drugInventoryService.getById(id);
        List<DrugInventoryDetail> list = drugInventoryDetailService.list(Wrappers.<DrugInventoryDetail>query().lambda().eq(DrugInventoryDetail::getInventoryNo, drugInventory.getInventoryNo()));
        drugInventoryService.dowonloadInventoryDetail(list, response);
    }

    @Log("审核")
    @ApiOperation("审核")
    @PostMapping(value = "/examine")
//    @PreAuthorize("@el.check('inventory:examine')")
    public ResponseEntity<Object> examine(@Validated @RequestBody InventoryExamineReq request) {
        drugInventoryService.examine(request);
        return new ResponseEntity<>(true, HttpStatus.OK);
    }

}
